package concurrencyPractice.chapter04;

import concurrencyPractice.GuardBy;
import concurrencyPractice.ThreadSafe;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 程序4-11 线程安全且可变的Point类
 * @date 2023/12/28 17:12:55
 */
@ThreadSafe
public class SafePoint {
    @GuardBy("this")
    private int x, y;

    private SafePoint(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public SafePoint(int[] a) {
        this(a[0], a[1]);
    }
    public SafePoint(SafePoint sp) {
        this(sp.get());
    }

    public synchronized int[] get() {
        return new int[] {x, y};
    }

    public synchronized void set(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
